package com.android.lovely.ui.widget

import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout
import androidx.core.view.isVisible
import com.android.lovely.R
import com.android.lovely.databinding.ViewRoleTitleBinding
import com.kotlin.extensions.inflater.LayoutInflaterExt.layoutInflater
import com.kotlin.extensions.view.ViewExt.clickable

/**
 *
 * @author why
 * @date 2025/4/5 21:09
 */
class CreateRoleTitleView(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {
    private val mBinding by lazy {
        ViewRoleTitleBinding.inflate(layoutInflater, this, false)
    }

    var listener: OnTitleClickListener? = null

    private var title = ""
    private var desc = ""
    private var desc2 = ""
    private var isMust = false
    private var hasSwitch = false
    private var isOpen = false

    init {
        addView(mBinding.root)
        val a = context.theme.obtainStyledAttributes(
            attrs,
            R.styleable.RoleTitleWidget,
            0, 0
        )

        try {
            title = a.getString(R.styleable.RoleTitleWidget_role_title) ?: ""
            desc = a.getString(R.styleable.RoleTitleWidget_role_desc) ?: ""
            desc2 = a.getString(R.styleable.RoleTitleWidget_role_desc_2) ?: ""
            isMust = a.getBoolean(R.styleable.RoleTitleWidget_role_must, false)
            hasSwitch = a.getBoolean(R.styleable.RoleTitleWidget_role_switch, false)
            isOpen = a.getBoolean(R.styleable.RoleTitleWidget_role_switch_open, false)
        } finally {
            a.recycle()
        }

        mBinding.tvTitle.text = title
        mBinding.ivMust.isVisible = isMust
        if (desc.isEmpty()) {
            mBinding.tvDesc.text = ""
            mBinding.tvDesc.isVisible = false
        } else {
            mBinding.tvDesc.text = desc
            mBinding.tvDesc.isVisible = true
        }

        if (desc2.isEmpty()) {
            mBinding.tvDesc2.text = ""
            mBinding.tvDesc2.isVisible = false
        } else {
            mBinding.tvDesc2.text = desc2
            mBinding.tvDesc2.isVisible = true
        }

        mBinding.tvSelect.isVisible = hasSwitch
        mBinding.tvSelect.isSelected = isOpen
        mBinding.tvSelect.clickable {
            isOpen = !isOpen
            mBinding.tvSelect.isSelected = isOpen
            this.listener?.onSelect(isOpen)
        }
    }

    fun setMust(isOpen: Boolean) {
        isMust = isOpen
        mBinding.ivMust.isVisible = isMust

    }

    fun changeSwitch(isOpen: Boolean) {
        this.isOpen = isOpen
        mBinding.tvSelect.isSelected = isOpen
        this.listener?.onSelect(isOpen)
    }

    fun getCheck(): Boolean {
        return mBinding.tvSelect.isSelected
    }

    fun addCallback(listener: OnTitleClickListener) {
        this.listener = listener
    }

    fun showDesc2(open: Boolean) {
        mBinding.tvDesc2.isVisible = open
    }

    fun setTitle(title: String) {
        this.title = title
        mBinding.tvTitle.text = title
    }

    interface OnTitleClickListener {
        fun onSelect(isOpen: Boolean)
    }
}